<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" destroy-on-close
        :visible.sync="visible" :before-close="closeDailog" width="40%">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="100px">
            <el-form-item label="分类" prop="type">
                <el-input v-model="dataForm.type" placeholder="分类" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="业务模块" prop="module">
                <ec-dict-select v-model="dataForm.module" placeholder="请选择业务模块" :disabled="true"
                    dictType="module_ident">
                </ec-dict-select>
            </el-form-item>
            <el-form-item label="名称" prop="label">
                <el-input v-model="dataForm.label" placeholder="名称"></el-input>
            </el-form-item>
            <el-form-item label="编号" prop="code">
                <el-input v-model="dataForm.code" placeholder="编号"></el-input>
            </el-form-item>
            <el-form-item label="图标" prop="icon">
                <d2-icon-select v-model="dataForm.icon" placeholder="图标" :userInput="true" style="width:100%">
                </d2-icon-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as catalogtreeService from '@/api/bdms/catalogtree'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    id: 0,
                    pid: '',
                    code: '',
                    label: '',
                    icon: '',
                    type: '',
                    module: '',
                    revision: '',
                },
                dataRule: {
                    pid: [{
                        required: true,
                        message: '父编号不能为空',
                        trigger: 'blur'
                    }],
                    code: [{
                        required: false,
                        message: '编号不能为空',
                        trigger: 'blur'
                    }],
                    label: [{
                        required: true,
                        message: '名称不能为空',
                        trigger: 'blur'
                    }],
                    icon: [{
                        required: true,
                        message: '图标不能为空',
                        trigger: 'blur'
                    }],
                    type: [{
                        required: true,
                        message: '分类不能为空',
                        trigger: 'blur'
                    }],
                    module: [{
                        required: true,
                        message: '业务模块不能为空',
                        trigger: 'blur'
                    }],
                }
            }
        },
        methods: {
            init(data) {
                this.dataForm.id = data.id || 0
                this.dataForm.pid = data.pid || 0
                this.dataForm.type = data.type || ''
                this.dataForm.module = data.module || ''
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        catalogtreeService.getcatalogtreeinfo(this.dataForm.id).then(data => {
                            this.dataForm.pid = data.pid
                            this.dataForm.code = data.code
                            this.dataForm.label = data.label
                            this.dataForm.icon = data.icon
                            this.dataForm.type = data.type
                            this.dataForm.module = data.module
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'id': this.dataForm.id || undefined,
                            'pid': this.dataForm.pid,
                            'code': this.dataForm.code,
                            'label': this.dataForm.label,
                            'icon': this.dataForm.icon,
                            'type': this.dataForm.type,
                            'module': this.dataForm.module,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.id ? catalogtreeService.addcatalogtree(params) :
                            catalogtreeService.updatecatalogtree(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>